package com.bstek.bdf2.core.context;

import com.bstek.bdf3.saas.domain.Organization;
import com.bstek.bdf3.security.ContextUtils;
import com.bstek.bdf3.security.orm.User;
import com.bstek.dorado.web.DoradoContext;

/**
 * 该类主要是为适配bdf2-export.jar
 * 
 * @author Administrator
 *
 */
public class ContextHolder {

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String beanId) {
		Object obj = DoradoContext.getAttachedWebApplicationContext().getBean(beanId);
		return (T) obj;
	}

	public static final String SESSION_USER_KEY = "ContextHolder_SESSION_USER_KEY";

	public static User getLoginUser() {
		return ContextUtils.getLoginUser();
//		return (User) DoradoContext.getCurrent().getAttribute(DoradoContext.SESSION, SESSION_USER_KEY);
	}
	
	public static String getLoginUsername(){
		User u = getLoginUser();
		if(u==null){
			return null;
		}
		return u.getUsername();
	}

	public static Organization getLoginOrganization() {
		User user = getLoginUser();
		if (user == null) {
			return null;
		}
		return user.getOrganization();
	}

	public static String getLoginOrgId() {
		Organization org = getLoginOrganization();
		return org == null ? null : org.getId();
	}
}
